function isArray(a){
    return Object.prototype.toString.apply(a) === '[object Array]';
}

function process_visnode(event, pathway){
	var nodeTypes = event.target.data.NodeTypes;
	if (nodeTypes == null){
		nodeTypes = event.target.data.type;
	}
	var mlabel = event.target.data.canonicalName;
	if (mlabel == null)
		mlabel  = event.target.data.label;
	nodeTypes = nodeTypes.toLowerCase();
	if (nodeTypes == "rxn"){			             
                   showBioEqn(mlabel);
                        }
                        if (nodeTypes == "met" || nodeTypes == "metabolite"){
				//var mlabel = event.target.data.canonicalName;
				//if (nodeTypes == "metabolite")
				//	mlabel = event.target.data.label;
                                showHostRxns(mlabel);
                        }
        if (nodeTypes == "gene"){
		var gene = event.target.data.label;//adipoflux_ids[event.target.data.canonicalName.split(".")[0]]
		showMetanode(gene, pathway);
		if (ec_dic[gene] != undefined){
			plotExpressions(gene);
        	}else{
			if (more_gexp[active_proj] != null && more_gexp[active_proj] == 0){
                		return;
        		}else{
				new Messi(gene + " does not have any expressions data" , {title:gene, titleClass:'info', autoclose:3000});
				mylayout.close('east');
			}					
		}				
        }
	if (nodeTypes == "mirna"){
		window.open(getMirbase(event.target.data.label));
	}
}

function getMirbase(mlabel){
/*
http://www.mirbase.org/cgi-bin/mirna_entry.pl?acc=MI0000085
miR-222 MI0000299     hsa-mir-222
miR-29a MI0000087     hsa-mir-29a 
miR-27a MI0000085     hsa-mir-27a
*/
	return "http://www.mirbase.org/cgi-bin/mirna_entry.pl?acc=" + mirna_acc[mlabel].split(":")[0];
}

function getUCSCEncode(){
        return 'ENCODE TF ChIP-seq signals@UCSC HUB<br><a target="_blank" href="http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeSydhTfbsHuvecCfosUcdSig">cFOS</a>' + '&nbsp;<a target="_blank" href="http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeSydhTfbsHuvecCjunStdSig">cJUN</a>' + '&nbsp;<a target="_blank" href="http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeSydhTfbsHuvecGata2StdSig">GATA2</a>'+ '&nbsp;<a target="_blank" href="http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeSydhTfbsHuvecMaxStdSig">MAX</a>'+ '&nbsp;<a target="_blank" href="http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeOpenChromChipHuvecCmycSig">cMYC</a><br>';
/*cFOS ChIP-seq signal (ENCODE)
cJUN ChIP-seq signal (ENCODE)
GATA2 ChIP-seq signal (ENCODE)
MAX ChIP-seq signal (ENCODE)
cMYC ChIP-seq signal (ENCODE)
http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeSydhTfbsHuvecCfosUcdSig
http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeSydhTfbsHuvecCjunStdSig
http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeSydhTfbsHuvecGata2UcdSig
http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeSydhTfbsHuvecMaxStdSig
http://genome-euro.ucsc.edu/cgi-bin/hgTrackUi?hgsid=194408644&g=wgEncodeOpenChromChipHuvecCmycSig
*/
}

function getUCSCBrowser(glabel){
	return '<a href="javascript:{}" onclick="launchUCSCBrowser(' + "'" + glabel + "'" + ')">Track Hub@UCSC Genome Browser</a>';
}

function launchUCSCBrowser(glabel){
        var geneinfo = adipoflux_genes[glabel];
	var gtk = geneinfo.split(":")
	var startstop = gtk[1];
	var start = parseInt(startstop.split("-")[0]) - 100000;
	if (start < 0)
		start = 0
	var stop = parseInt(startstop.split("-")[1]) + 100000
	geneinfo = gtk[0] + ":" + start + "-" + stop 
	new Messi('Copy ' + glabel + ' position ' + geneinfo +  ' and click:<br><a href="http://genome.ucsc.edu/cgi-bin/hgHubConnect?hubUrl=http%3A%2F%2Fsystemsbiology.uni.lu%2Fsgbs_hub%2Ftracks_adipo.txt&hgHub_do_reset=on&hgHubConnect.remakeTrackHub=on" target="_blank">UCSC Genome Browser Adipoflux Hub</a><br> for ChIP-seq signal tracks from PPAR studies in SGBS cells and primary adipocytes, CEBP and LXR from SGBS adipocytes, and H3K4me3 from primary and SGBS cells. See ' + '<a href="http://adipoflux.googlecode.com/files/user_guide.pdf">user guide</a> for more details.' , {title:"Adipocyte ChipSeq UCSC Genome Tracks", titleClass:'info'});
	//window.open('http://genome.ucsc.edu/cgi-bin/hgTracks?position=' + geneinfo);
        //return '<br><a href="http://genome.ucsc.edu/cgi-bin/hgTracks?position=' + geneinfo + '" target="_blank">UCSC Genome Browser</a>';
}

function showHostRxns(mets){
	var metobj = mets_details_dic[mets];
	var rxns = metobj.split(":")[1].split(",");
	var rxnstr = "";
	//'<a href="javascript:{}" onclick="plotExpressions(' + "'" + gene + "'" + ')">Expression Changes</a>';
	var i = 1;
	var br = "<br>";
	for (var rxn in rxns){
		br = "";
		if (i%4 == 0)
			br = "<br>";
		i = i + 1;		
		rxnstr = rxnstr + " " + br + '<a href="javascript:{}" onclick="showBioEqn(' + "'" + rxns[rxn] + "'" + ')">' + rxns[rxn] + '</a>';
	}
	new Messi(rxnstr, {title:mets, titleClass:'info', autoClose:4000});
}

function showBioEqn(rxn){
	var rxn_rec = rxn_details_dic[rxn.trim()].split("|");
	//RxnAbbr Subsystem RxnLongName Metabolite Short Long Genes EC Number 
	new Messi('Subsystem: ' + rxn_rec[0] + '<br>Name:' + rxn_rec[1] + '<br>Short formula: ' + rxn_rec[3] + '<br>Full formula: ' + rxn_rec[4] + '<br>Metabolites:' + rxn_rec[2] + '<br>Genes:' + rxn_rec[5] + '<br>EC numbers:' + rxn_rec[6], {title:rxn, titleClass:'info'});
}

function showBlockedReactions(gene, pathway){
	var geneId = adipoentrez_ids[gene];
	var brRxns = blocked_reactions[pathway.split("_sif")[0] + "_" + geneId]; 
	if (brRxns == undefined){
		return "";
	}	
	return "<br><b>Blocked:</b><br><font size='-1'>" + brRxns + "</font>";
}

function showMetanode(gene, pathway){
	var legend = "images/legend.png";
	var legend_sub = "images/legend_sub.png";
	if (pathway == undefined && active_proj == "adipocyte"){
	//if (active_proj == "adipocyte"){
	    pathway = "cholesterol";
	}else if (active_proj == "adipocyte"){
	    pathway = pathway;
	}else{//indicates huvec
	    if (pathway != "huvec_argpro"){
		pathway = pathway;
	    }	
	}
	var imagenodes = null;
	if (pathway.indexOf("huvec") != -1){
		legend = "images/huvec_legend.png"
	}
	if (pathway == "cholesterol"){
imagenodes = {
"19": { image:'images/cholesterol/19_CHSTEROLt.png'},
"39": { image: 'images/cholesterol/39_ACACT1.png'},
"1595": { image: 'images/cholesterol/1595_LNS14DMr.png'},
"1717": { image: ['images/cholesterol/1717_DHCR71r_DHCR72r.png','images/cholesterol/1717_DHCR71r.png','images/cholesterol/1717_DHCR72r.png']},
"1718": { image: ['images/cholesterol/1718_DHCR241r_DHCR242r_DHCR243r.png','images/cholesterol/1718_DHCR241r.png','images/cholesterol/1718_DHCR242r.png','images/cholesterol/1718_DHCR243r.png']},
"2222": { image: 'images/cholesterol/2222_SQLSr.png'},
"2224": { image: 'images/cholesterol/2224_cholesterol_rxns.png'},
"3155": { image: ['images/cholesterol/3155_HMGLm_HMGLx.png','images/cholesterol/3155_HMGLm.png','images/cholesterol/3155_HMGLx.png']},
"3156": { image: 'images/cholesterol/3156_HMGCOARx.png'},
"3157": { image: 'images/cholesterol/3157_HMGCOASi.png'},
"3158": { image: 'images/cholesterol/3158_HMGCOASim.png'},
"6713": { image: 'images/cholesterol/6713_SQLE_rxn_SQLEr.png'},
"3295": { image: 'images/cholesterol/3295_C3STKR2r.png'},
"3422": { image: 'images/cholesterol/3422_IPDDIx.png'},
"4047": { image: 'images/cholesterol/4047_LNSTLSr.png'},
"4597": { image: 'images/cholesterol/4597_DPMVDx.png'},
"4598": { image: 'images/cholesterol/4598_MEVK1x.png'},
"6307": { image: ['images/cholesterol/6307_C4STMO1r_C4STMO2r.png','images/cholesterol/6307_C4STMO1r.png','images/cholesterol/6307_C4STMO2r.png']},
"6309": { image: 'images/cholesterol/6309_cholesterol_rxns.png'},
"6646": { image: 'images/cholesterol/6646_cholesterol_rxns.png'},
"6713": { image: 'images/cholesterol/6713_SQLEr.png'},
"7108": { image:'images/cholesterol/7108_C14STRr.png'},
"8034": { image:'images/cholesterol/8034_COAtm.png'},
"10654": { image: 'images/cholesterol/10654_PMEVKx.png'},
"10682": { image: ['images/cholesterol/10682_EBP1r_EBP2r.png','images/cholesterol/10682_EBP1r.png','images/cholesterol/10682_EBP2r.png']},
"50814": { image: 'images/cholesterol/50814_cholesterol_rxns.png'},
"54511": { image:'images/cholesterol/54511_HMGLm.png'},
"91734": { image: 'images/cholesterol/91734_IPDDIx.png'}
           };
        }
	if (pathway.indexOf("_sif") != -1){
		imagenodes = sif_imagenodes;
	}

	if (pathway == "faa"){
                imagenodes = {
                 "34": { image:'images/faa/34_FAOXC161802m.png'},
                 "35": { image:'images/faa/35_FAOXC161802m.png'},
                 "8034": { image:'images/faa/8034_COAtm.png'},
                 "11001": { image:'images/faa/11001_FACOAL191.png'},
                 "51703": { image:'images/faa/51703_rxns.png'},
                 "116285": { image:'images/faa/116285_FACOAL40im.png'},
                 "23305": { image:['images/faa/23305_rxns.png','images/faa/23305_FACOAL150_FACOAL170.png','images/faa/23305_FACOAL160i_FACOAL180i_FACOAL204i.png']},
                 "2181": { image:['images/faa/2181_rxns.png','images/faa/2181_FACOAL140i_FACOAL80i.png','images/faa/2181_FACOAL150_FACOAL170.png','images/faa/2181_FACOAL160i_FACOAL180i_FACOAL181i_FACOAL204i.png']},
                 "2180": { image:['images/faa/2180_rxns.png','images/faa/2180_FACOAL150_FACOAL170.png','images/faa/2180_FACOAL160i_FACOAL180i_FACOAL181i_FACOAL204i.png','images/faa/2180_FACOAL161.png','images/faa/2180_FACOAL1812_FACOAL1813.png','images/faa/2180_FACOAL1821_FACOAL1832_FACOAL200.png','images/faa/2180_FACOAL1822_FACOAL204_FACOAL226.png','images/faa/2180_FACOAL1831_FACOAL184_FACOAL205_FACOAL224_FACOAL246_1.png']},
                 "2182": { image:['images/faa/2182_rxns.png','images/faa/2182_FACOAL150_FACOAL170.png','images/faa/2182_FACOAL204i.png']}
                };
        }
        if (pathway == "fao"){
	        imagenodes = {
                 "34": { image:['images/fao/34_rxns.png','images/fao/34_FAOXC80.png','images/fao/34_FAOXC140_FAOXC150m_FAOXC160.png']},
                 "35": { image:['images/fao/35_rxns.png','images/fao/35_FAOXC80.png','images/fao/35_FAOXC140_FAOXC150m_FAOXC160.png']}
                };
        }
	if (pathway == "huvec_argpro"){
                imagenodes = {
		'10165':{image:'images/huvec/ArgProMet/10165.png'},
'10166':{image:'images/huvec/ArgProMet/10166.png'},
'112483':{image:'images/huvec/ArgProMet/112483.png'},
'113451':{image:'images/huvec/ArgProMet/113451.png'},
'1610':{image:'images/huvec/ArgProMet/1610.png'},
'216':{image:'images/huvec/ArgProMet/216.png'},
'217':{image:'images/huvec/ArgProMet/217.png'},
'219':{image:'images/huvec/ArgProMet/219.png'},
'223':{image:'images/huvec/ArgProMet/223.png'},
'224':{image:'images/huvec/ArgProMet/224.png'},
'262':{image:'images/huvec/ArgProMet/262.png'},
'26':{image:'images/huvec/ArgProMet/26.png'},
'2805':{image:'images/huvec/ArgProMet/2805.png'},
'2806':{image:'images/huvec/ArgProMet/2806.png'},
'314':{image:'images/huvec/ArgProMet/314.png'},
'4128':{image:'images/huvec/ArgProMet/4128.png'},
'4129':{image:'images/huvec/ArgProMet/4129.png'},
'4507':{image:'images/huvec/ArgProMet/4507.png'},
'4842':{image:'images/huvec/ArgProMet/4842.png'},
'4843':{image:'images/huvec/ArgProMet/4843.png'},
'4844':{image:'images/huvec/ArgProMet/4844.png'},
'4845':{image:'images/huvec/ArgProMet/4845.png'},
'4846':{image:'images/huvec/ArgProMet/4846.png'},
'501':{image:'images/huvec/ArgProMet/501.png'},
'5033':{image:'images/huvec/ArgProMet/5033.png'},
'5034':{image:'images/huvec/ArgProMet/5034.png'},
'5625':{image:'images/huvec/ArgProMet/5625.png'},
'5831':{image:'images/huvec/ArgProMet/5831.png'},
'58510':{image:'images/huvec/ArgProMet/58510.png'},
'6303':{image:'images/huvec/ArgProMet/6303.png'},
'79751':{image:'images/huvec/ArgProMet/79751.png'},
'79814':{image:'images/huvec/ArgProMet/79814.png'},
'83733':{image:'images/huvec/ArgProMet/83733.png'},
'83884':{image:'images/huvec/ArgProMet/83884.png'},
'8402':{image:'images/huvec/ArgProMet/8402.png'},
'8604':{image:'images/huvec/ArgProMet/8604.png'},
'8639':{image:'images/huvec/ArgProMet/8639.png'},
'8659':{image:'images/huvec/ArgProMet/8659.png'},
'8854':{image:'images/huvec/ArgProMet/8854.png'},
'8974':{image:'images/huvec/ArgProMet/8974.png'}};
        }
	if (pathway == "tag"){
                imagenodes = {
                 "2819": { image:'images/tag/2819_G3PD1.png'},
                 "3990": { image:'images/tag/3990_LPSe.png'},
                 "4023": { image:'images/tag/4023_LPS_LPS2.png'},
                 "5406": { image:'images/tag/5406_LPSe.png'},
                 "5407": { image:'images/tag/5407_LPSe.png'},
                 "5408": { image:'images/tag/5408_LPSe.png'},
                 "8513": { image:'images/tag/8513_LPSe.png'},
                 "8694": { image:'images/tag/8694_DGAT.png'},
                 "9388": { image:'images/tag/9388_LPSe.png'},
                 "10554": { image:'images/tag/10554_AGPAT1.png'},
                 "10555": { image:'images/tag/10555_AGPAT1.png'},
                 "11343": { image:'images/tag/11343_LPS3.png'},
                 "23175": { image:'images/tag/23175_PPAP.png'},
                 "50487": { image:'images/tag/50487_LPS4e.png'},
                 "55326": { image:'images/tag/55326_AGPAT1.png'},
                 "56894": { image:'images/tag/56894_AGPAT1.png'},
                 "56895": { image:'images/tag/56895_AGPAT1.png'},
                 "57678": { image:'images/tag/57678_GPAM_hs.png'},
                 "80168": { image:'images/tag/80168_MOGAT.png'},
                 "84649": { image:'images/tag/84649_DGAT.png'},
                 "116255": { image:'images/tag/116255_MOGAT.png'},
                 "137964": { image:'images/tag/137964_AGPAT1.png'}
                };
        }
       if (pathway == "valleuiso"){
                imagenodes = {
                 "18": { image:'images/valleuiso/18_3AIBTm.png'},
                 "34": { image:['images/valleuiso/34_ACOAD10m_ACOAD9m.png','images/valleuiso/34_ACOAD9m.png','images/valleuiso/34_ACOAD10m.png']},
                 "36": { image:'images/valleuiso/36_ACOAD10m.png'},
                 "38": { image:'images/valleuiso/38_ACACT10m.png'},
                 "549": { image:'images/valleuiso/549_MGCHrm.png'},
                 "586": { image:'images/valleuiso/586_ILETA_LEUTA_VALTA.png'},
                 "587": { image:['images/valleuiso/587_ILETAm_LEUTAm_VALTAm.png','images/valleuiso/587_ILETAm.png','images/valleuiso/587_LEUTAm.png','images/valleuiso/587_VALTAm.png']},
                 "593": { image:['images/valleuiso/593_OIVD1m_OIVD2m_OIVD3m.png','images/valleuiso/593_OIVD1m.png','images/valleuiso/593_OIVD2m.png','images/valleuiso/593_OIVD3m.png']},
                 "594": { image:['images/valleuiso/594_OIVD1m_OIVD2m_OIVD3m.png','images/valleuiso/594_OIVD1m.png','images/valleuiso/594_OIVD2m.png','images/valleuiso/594_OIVD3m.png']},
                 "1629": { image:['images/valleuiso/1629_OIVD1m_OIVD2m_OIVD3m.png','images/valleuiso/1629_OIVD1m.png','images/valleuiso/1629_OIVD2m.png','images/valleuiso/1629_OIVD3m.png']},
                 "1738": { image:['images/valleuiso/1738_OIVD1m_OIVD2m_OIVD3m.png','images/valleuiso/1738_OIVD1m.png','images/valleuiso/1738_OIVD2m.png','images/valleuiso/1738_OIVD3m.png']},
                 "1892": { image:['images/valleuiso/1892_ECOAH12m_ECOAH9m.png','images/valleuiso/1892_ECOAH9m.png','images/valleuiso/1892_ECOAH12m.png']},
                 "3028": { image:'images/valleuiso/3028_HACD9m.png'},
                 "3030": { image:['images/valleuiso/3030_ECOAH12m_ECOAH9m.png','images/valleuiso/3030_ECOAH9m.png','images/valleuiso/3030_ECOAH12m.png']},
                 "3032": { image:['images/valleuiso/3032_ECOAH12m_ECOAH9m.png','images/valleuiso/3032_ECOAH9m.png','images/valleuiso/3032_ECOAH12m.png']},
                 "3155": { image:'images/valleuiso/3155_HMGLm.png'},
                 "3712": { image:'images/valleuiso/3712_ACOAD8m.png'},
                 "4329": { image:'images/valleuiso/4329_MMSAD1m.png'},
                 "4594": { image:'images/valleuiso/4594_MMMm.png'},
		 "5019": {image:'images/valleuiso/5019_OCOAT1m.png'},
                 "5095": { image:'images/valleuiso/5095_PPCOACm.png'},
                 "5096": { image:'images/valleuiso/5096_PPCOACm.png'},
                 "8034": { image:'images/valleuiso/8034_COAtm.png'},
                 "11112": { image:'images/valleuiso/11112_HIBDm.png'},
                 "23417": { image:'images/valleuiso/23417_MMCDm.png'},
                 "27034": { image:'images/valleuiso/27034_ACOAD9m.png'},
                 "56922": { image:'images/valleuiso/56922_MCCCrm.png'},
                 "54511": { image:'images/valleuiso/54511_HMGLm.png'},
                 "64064": { image:'images/valleuiso/64064_OCOAT1m.png'},
                 "64087": { image:'images/valleuiso/64087_MCCCrm.png'},
                 "84693": { image:'images/valleuiso/84693_MMEm.png'}
                };
        }
	var genepic = "";
	gene = gene.split(".")[0];
	var isArr = false;	
	var imagefile = "";
	var sifimages = false;
	var spRxns = null;
	for (var n in imagenodes){
		imagefile = imagenodes[n]["image"];
		spRxns = imagenodes[n]["spRxns"];
		var id = adipoentrez_ids[gene];
		var _brk = "";
		if (isArray(imagefile) == false){			
			if (imagefile.indexOf(id) != -1){
				if (imagefile.indexOf("_RXNS") != -1){
                        		//imagefile = ['images/Alaninean/8604_RXNS.png', 'images/Alaninean/8604_GLUt2m.png', 'images/Alaninean/8604_ASPGLUm.png', 'images/Alaninean/8604_3SALAASPm.png'];
                        		sifimages = true;
                			break;
				}
				imagefile = imagefile.replace("_icon", "");
				genepic = '<img src="' + imagefile + '" alt="' + gene + '"/><br>Reactions:<br>';
				var itk = imagefile.split("/");
				var png = itk[itk.length - 1];
				var rxns = png_rxn[png];
			        if (pathway == "tag"){
					rxns = genes_rxn_dic[gene]; 
					rxns = rxns.split(",");
				}
        			var rxn_links = "";
        			if (isArray(rxns)){
					for (var ri = 0; ri< rxns.length; ri++){
						_brk = "";
                                                if (ri > 1 && ri%2 == 0)
                                                	_brk = "<br>"
                				rxn_links = rxn_links + _brk + '<a href="javascript:{}" onclick="showBioEqn(' + "'" + rxns[ri].replace(" ", "") + "')" + '">' + rxns[ri] + '</a>&nbsp;'
        				}
				}else{
					if (rxns == undefined){
						rxns = genes_rxn_dic[gene];
						if (pathway == 'valleuiso' || pathway == 'cholesterol'){
							rxns = png.split(id + "_")[1];
							rxns = rxns.substring(0,rxns.length-4);
						}
						
						if (rxns.indexOf(",") == -1){
							rxn_links = rxn_links + '<a href="javascript:{}" onclick="showBioEqn(' + "'" + rxns.replace(" ", "")     + "')" + '">' + rxns + '</a>&nbsp;'
						}else{
							var ltk = rxns.split(",");
							for (var k in ltk){
								rxn_links = rxn_links + '<a href="javascript:{}" onclick="showBioEqn(' + "'" + ltk[k] + "')" + '">' + ltk[k] + '</a>&nbsp;'
							}
						}	
						
					}
					//rxn_links = rxn_links + '<a href="javascript:{}" onclick="showBioEqn(' + "'" + rxns.replace(" ", "") + "')" + '">' + rxns + '</a>&nbsp;'
				}
				genepic = genepic + rxn_links;
				break;
			}
		}else{
			genepic = "";
			isArr = true;
			//first image will not have reactions listed
			if (id == n || sifimages){
				genepic = '<img src="' + imagefile[0] + '" alt="' + gene + '"/><br>Reactions:<br>';	
				for (var ii = 1; ii< imagefile.length; ii++){
					var itk = imagefile[ii].split("/");
	                                var png = itk[itk.length - 1];
        	                        var rxns = png_rxn[png];
					if (pathway == "tag"){
                                        	rxns = genes_rxn_dic[gene];
                                        	rxns = rxns.split(",");
                                	}
                        	        var rxn_links = "";
                                	if (isArray(rxns)){
                                        	for (var ri = 0; ri< rxns.length; ri++){
							_brk = "";
                                                	if (ri > 1 && ri%2 == 0)
								_brk = "<br>"
							rxn_links = rxn_links + _brk + '<a href="javascript:{}" onclick="showBioEqn(' + "'" + rxns[ri].replace(" ", "") + "')" + '">' + rxns[ri] + '</a>&nbsp;'
                                        	}
                                	}else{
						//break;
                                        	rxn_links = rxn_links + '<a href="javascript:{}" onclick="showBioEqn(' + "'" + rxns.replace(" ", "") + "')" + '">' + rxns + '</a>&nbsp;'
                                	}
					genepic = genepic + '<img src="' + imagefile[ii] + '" alt="' + gene + '"/><br>';
					genepic = genepic + rxn_links; 
				}
				break;
			}
		}
	}
	if (sifimages == true){
		for (var li = 0; li< spRxns.length; li++){
			var rxnName = spRxns[li].split("_")[1].split(".")[0];
			genepic = genepic + '<img src="' + spRxns[li] + '" alt="' + gene + '"/><br>' + '<a href="javascript:{}" onclick="showBioEqn(' + "'" + rxnName.replace(" ", "") + "')" + '">' + rxnName + '</a>';
		}
	}
	var gexp_tf = '<a href="javascript:{}" onclick="plotExpressions(' + "'" + gene + "'" + ')">Expression Changes</a>';
	if (ec_dic[gene] == undefined || pathway.indexOf("huvec") != -1){
        	gexp_tf = "";
        }     
	if (imagefile.length >= 1 ){ //&& isArr){
		var ucsclink = getUCSCBrowser(gene) + "<br>";
		if (pathway.indexOf("huvec") != -1 && active_proj == "huvec"){
			ucsclink = getUCSCEncode();
		}
			
		$('#legend_image').html(
		"<b>" + gene + " " + gexp_tf + "</b><br>" + ucsclink + genepic + showBlockedReactions(gene, pathway) + "<br><Legend><b>Legend:</b><br>" + '<img src="' + legend + '" alt="adipoflux_legend"/><br>' + '<img src="' + legend_sub + '" alt="adipoflux_legend"/><br>');
		mylayout.close('west');
        	mylayout.sizePane("west", 300);
        	mylayout.open('west');
	}else{
		new Messi('<a href="javascript:{}" onclick="showLegend()">Legend</a><br>' + genepic,
		{title:adipoflux_ids[gene], titleClass:'info', /*autoclose:5000,*/ height:"auto"});
	}
}

function showtip(pathway, evt, divid){
	var mdata = evt.target.data;
        var xoffset = 0;
	var yoffset = 0;
	var eastopen = !mylayout.state.east.isClosed;
	var westopen = !mylayout.state.west.isClosed;
	if (westopen)
		xoffset = 150;
	if (eastopen && westopen)
		xoffset = 100;
	var mouseX = evt.target.x + 50 + xoffset;// 25;// $('#'+divid).offset().left;
        var mouseY = evt.target.y + 100;// + 25; //$('#'+divid).offset().top - 10;
        var maxW = 100;
	var nodeType = mdata.NodeTypes;
	if (nodeType == null){
		nodeType = mdata.type;
	}
	var t_text = "";
	nodeType = nodeType.toLowerCase();
	if (nodeType == "met" || nodeType == "metabolite"){
		t_text = "Metabolite:" + mdata.label            
        + '<br><a href="javascript:{}" onclick="showHostRxns(' + "'" + mdata.label + "'" + ')">Details</a>';
	}else
	if (nodeType == "rxn"){
		if (mdata.canonicalName == null)
			mdata.canonicalName = mdata.label;
                t_text = "Rxn:" + mdata.canonicalName + '<br><a href="javascript:{}" onclick="showBioEqn(' + "'" + mdata.canonicalName + "')" + '">Reaction Details</a>';
        }else
	if (nodeType == "gene"){
		var glabel = mdata.label;//adipoflux_ids[mdata.label.split(".")[0]];
		var geneinfo = adipoflux_genes[glabel];
		var ec_link = '<br><a href="javascript:{}" onclick="plotExpressions(' + "'" + glabel + "')" + '">Expression Changes</a>'; 
		if (ec_dic[glabel] == undefined){
                	ec_link = "";
        	}
		var ucsclink = '<br>' + getUCSCBrowser(glabel);
		if (pathway.indexOf("huvec") != -1){
			ucsclink = '';
			   ec_link = ''; 
		}	
        	t_text = glabel
		+ '<br><a href="javascript:{}" onclick="showMetanode(' + "'" + mdata.label + "','" + pathway +  "')" + '">Metanode Details</a>' 
		+ ec_link + ucsclink;
		//+ '<br><a href="javascript:{}" onclick="showCatRxn(' + "'" + mdata.label + "')" + '">Catalyzed Reactions</a>'
		//+ '<br>' + getUCSCBrowser(glabel);
        }else if (nodeType == "mirna"){
		t_text = "miRNA " + mirna_acc[mdata.label];
	}    
	$("body").qtip({
                        content: {
                            text: t_text,
                        },
                        position: {
                            type: "absolute",
                            corner: {
                                tooltip: "bottomLeft",
                                target: "topLeft"
                            },
                            adjust: {
                                mouse: false,
                                x: mouseX,
                                y: mouseY,
                                scroll: false,
                                resize: false
                            }
                        },
			show: {
                    delay: 0,
                    when: false,
		    solo: true,
                    effect: { type: "fade", length: 0 },
                    ready: true // Show the tooltip when ready
                },
			hide: {
                    delay: 0,
                    effect: { type: "fade", length: 0 },
                    when: { event: "unfocus" }, // Hide when clicking anywhere else
                    fixed: true // Make it fixed so it can be hovered over
                },
                        style: {
                           border: { width: 1, radius: 8 },
                           textAlign: 'left',
                           name: 'blue', // Style it according to the preset 'cream' style,
                           tip: true      // Give it a speech bubble tip with automatic corner detection
                        }
                    });
}


function loadPathwayCyWeb(pathway, div_id){
	if (pathway.indexOf("huvec") != -1){
        	active_proj = "huvec";
        }else{
        	active_proj = "adipocyte";
        }
	mylayout.close('east');
	mylayout.close('west');
	var options = {
                    // where you have the Cytoscape Web SWF
                    swfPath: "/swf/CytoscapeWeb",
                    // where you have the Flash installer SWF
                    flashInstallerPath: "/swf/playerProductInstall"
         };
	if ($("#searchterm-select").val() == "")
		adipo_searchterm = "";

	// init and draw
        cywebvis = new org.cytoscapeweb.Visualization(div_id, options);
        cywebvis.addListener("click", "nodes", function(event) {        
                handle_node_click(event);
        });
  	function handle_node_click(event) {
                process_visnode(event, pathway);
        }
        cywebvis.addListener("mouseover", "nodes", function(evt){
        	showtip(pathway, evt, div_id);
        });
        // Change the labels of selected nodes and edges:
        cywebvis.ready(function (){
                cywebvis.visualStyleBypass(getVisualByPass(pathway, cywebvis.nodes(), adipo_searchterm));
        });
	//find non xgml pathways
	var gschema = {
        nodes: [ { name: "label", type: "string" },
                 { name: "score", type: "number" },
		{ name: "col", type: "string" },
		{ name: "hl", type: "string" },
		{ name: "type", type: "string" },
		{ name: "image", type: "string"} ],

        edges: [ { name: "label", type: "string" },
                 { name: "directed", type: "boolean", defValue: true}, 
		{ name: "col", type: "string" },
		{ name: "arr", type: "string" },
		{ name: "thick", type: "string" },
		{ name: "dash", type: "string" } ]
    	};
	var sifnodes = null;
	var siflinks = null;
	
	if (pathway.indexOf("_sif") != -1){
		pk = pathway.split("_sif")[0]
		if (pathway.indexOf("adipo") != -1){
			sifnodes = adipocytesifdata[pk+"_nodes"];
                	siflinks = adipocytesifdata[pk+"_links"];
		}else if (pathway.indexOf("huvec") != -1){
			sifnodes = huvecsifdata[pk+"_nodes"];
                        siflinks = huvecsifdata[pk+"_links"];
		}
		var pathwayEncoded = pathway.split("_sif")[0];
		
		var network = {
    		dataSchema: gschema, 
    		data: {
        	nodes: sifnodes,
		edges: siflinks
		}
		};
		var layoutname = $('#siflayout').val();
		var radial = {
    			name:    layoutname,
    			options: { angleWidth: 180, radius: 80 }
		};
                var visual_style = {
                    global: {backgroundColor: "white"},
                    nodes: { 
                        shape: {discreteMapper : shapeMapper},
                        borderWidth: 1,
                        borderColor: "BLACK",
			//borderWidth: {discreteMapper: borderWidthMapper},
			//borderColor: {discreteMapper: borderColorMapper},
                        size: {discreteMapper : sizeMapper},
			height: {discreteMapper : heightMapper},
                        color: {discreteMapper: colorMapper},
			labelFontSize: {discreteMapper: labelSizeMapper},
			labelFontWeight: {discreteMapper: labelWeightMapper},
                        labelHorizontalAnchor: "center"
                    },
                    edges: {
			style: {discreteMapper: dashMapper},
                        width: {discreteMapper: widthMapper},
			sourceArrowShape: {discreteMapper: arrowSourceMapper},
			targetArrowShape: {discreteMapper: arrowTargetMapper},
                        color: {discreteMapper: edgeColorMapper}
                    }
                };
		cywebvis.draw({ network: network, layout: radial, visualStyle:visual_style});
	}else{
        	$.get("gml/" + pathway + "_phatviz.xgmml", function(data) {
        	if (typeof data !== "string") {
                	if (window.ActiveXObject) { // IE
                        	data = data.xml;
                        } else {
                        	data = (new XMLSerializer()).serializeToString(data);
                       }
                }
        	cywebvis.draw({ network: data, layout: "Preset" });
        	});
	}	
}
